package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cg.domain.ZMCGChangeWorkDetail;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanWM;

/**
 * <p>Title: ServiceZMCP02.java</p>
 * <p>Description: 交接班记录明细</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张坤
 * @version 1.0
 * <pre>Histroy:
 *          2016-03-21 下午02:51:04  张坤  Create
 *</pre>
 */
public class ServiceZMCG4001 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午02:51:38 张坤
	 * </p>
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}	
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午02:51:58 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
		String fdChangeId = inInfo.getString("fdChangeId");
		EiInfo outInfo = inInfo;
		if(fdChangeId != null)
		{ 
			StaticTools.setQueryParameters(inInfo, "fdChangeId", fdChangeId, true);//添加查询条件
			outInfo = super.query(inInfo, "ZMCGChangeWorkDetail.query", new ZMCGChangeWorkDetail());
		}	
		else{
			outInfo.addBlock("result").setBlockMeta(new ZMCPProducePlanWM().eiMetadata);
		}
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", fdMiningId, true));		 
		return outInfo;
	}
		
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午03:07:57 张坤
	 * </p>
	 *
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		
		//校验是否重复
		EiInfo out = new EiInfo(); 
		ZMCGChangeWorkDetail _dom = new ZMCGChangeWorkDetail();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_CHANGEWORK_DETAIL"));
			_dom.setFdChangeId(inInfo.getString("fdChangeId"));
			String fdCurrentDate = inInfo.getString("fdCurrentDate");
			if(fdCurrentDate == null || fdCurrentDate.trim().equals("")) {
				fdCurrentDate = StaticTools.getDate();
			}
			_dom.setFdCurrentDate(fdCurrentDate);
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(), _dom.getFdWorknumId()));
			
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdCurrentDate", _dom.getFdCurrentDate());
			_map.put("fdWorknumId", _dom.getFdWorknumId());
			boolean validate = this.validate(_dom, "ZMCGChangeWorkDetail.validate", _map);
			
			if(validate) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("每班只能有一条记录！");
				return out;
			}
			
			this.dao.insert("ZMCGChangeWorkDetail.insert",_dom);
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}		
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午04:20:50 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验是否重复
		EiInfo out=new EiInfo(); 
		ZMCGChangeWorkDetail _dom = new ZMCGChangeWorkDetail();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			String fdCurrentDate = inInfo.getString("fdCurrentDate");
			if(fdCurrentDate == null || fdCurrentDate.trim().equals("")) {
				fdCurrentDate = StaticTools.getDate();
			}
			_dom.setFdCurrentDate(fdCurrentDate);
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(), _dom.getFdWorknumId()));
			
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("fdCurrentDate", _dom.getFdCurrentDate());
			_map.put("fdWorknumId", _dom.getFdWorknumId());
			_map.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = this.validate(_dom, "ZMCGChangeWorkDetail.validate", _map);
			
			if(validate) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("每班只能有一条记录！");
				return out;
			}
			
			this.dao.insert("ZMCGChangeWorkDetail.update",_dom);
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}	
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午04:30:17 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) { 
		EiInfo _temp = super.delete(inInfo, "ZMCGChangeWorkDetail.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo; 
	}
	
	@SuppressWarnings("rawtypes")
	private boolean validate(ZMCGChangeWorkDetail _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		List result = dao.query(sqlName, params);
		if(result != null && result.size() > 0 ) {
			return true;
		}		
		return false;
	}
}
